
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import '../../entity/reader_entity.dart';
import '../../http/api.dart';
import '../../main.dart';

class ListModel{

  final RxList<ReaderCurrentBorrowList> _list = RxList();
  final _allList = <ReaderCurrentBorrowList>[];
  bool showButton = false;
  String filter = '';
  String keyword = '';


  filterList(filter){
    this.filter = filter;
    _list.clear();
    if(filter.isEmpty){
      _list.addAll(_allList);
      return;
    }
    for(var element in _allList) {
      if (element.toString().contains(filter)) {
        _list.add(element);
      }
    }
  }

  _listCell(text,{int width = 90, int height = 20, Color color =Colors.black }){
    return Container(
        alignment: Alignment.center,
        width: width.w,
        height: height.h,
        child: Text('$text',style: TextStyle(color:color, fontSize: 8.sp),textAlign: TextAlign.center,)
    );
  }

  _labelRow(radius){
    return  Container(
      height: 30.h,
      decoration:  BoxDecoration(
        color: const Color.fromRGBO(219, 232, 255, 1),
        borderRadius: BorderRadius.only(topLeft:Radius.circular(radius),topRight:Radius.circular(radius)),
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.start,
        children: [
          _listCell("序号",width: 50),
          _listCell("书籍名称",width: 140),
          _listCell("书籍编号",width: 140),
          _listCell("应还时间",width: 100),
          _listCell(showButton?"操作":"状态",width: 80),
        ],
      ),
    );
  }

  Widget show(list,{bool showButton = false,bool showSearch = false}){
    this.showButton = showButton;
    _list.clear();
    _allList.clear();
    _list.addAll(list);
    _allList.addAll(list);
    return  Obx(() => Container(
      width: 510.w,
      height: 310.h,
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.all(Radius.circular(4.r)),
      ),
      margin: EdgeInsets.only(left: 10.w),
      child: Column(
        children: [
          if(showSearch)...[
            _searchBarWidget(),
          ],
          _labelRow(showSearch?0.r:4.r),
          SizedBox(height:4.h),
          SizedBox(
            height: showSearch == true?220.h:270.h,
            child: ListView.separated(
              itemBuilder: (context, index) {
                return _itemBuilder(index);
              },
              itemCount: _list.length,
              separatorBuilder: (BuildContext context, int index) {
                return Divider(
                  color: Colors.grey[400],
                );
              },
            )
          ),
        ],
      )
    ));
  }

  _itemBuilder(index){
    var item = _list[index];
    return Row(
      children: [
        _listCell("${index+1}",width: 50),
        _listCell("《${item.bookTitle}》",width: 140),
        _listCell("${item.bookNo}",width: 140),
        _listCell(item.readyReturnDate??'',width: 100),
        Container(
          width: 80.w,
          alignment: Alignment.center,
          child:  showButton?
            Tooltip(
              message: item.error??'',
              child:  InkWell(
                onTap: () async {
                  if(item.status == 0){
                    var result = await bookBorrowAgain(currentTask.uid,item.bookNo);
                    if(result.$1 == 200){
                      tts.speak('续借成功');
                      item.error = '续借成功';
                      item.status = 2;
                      item.readyReturnDate = result.$2.readyReturnDate;
                      _list.removeAt(index);
                      _list.insert(index, item);
                    }else{
                      tts.speak('续借失败');
                      item.error = result.$2;
                      item.status = 1;
                      _list.removeAt(index);
                      _list.insert(index, item);
                    }
                  }
                },
                child: Image.asset("assets/images/${item.status == 0?'borrow_again':item.status == 1?'borrow_again_fail':'borrow_again_success'}.png",width: 60.w,fit: BoxFit.fitWidth,)
              ),
            )
            :Image.asset("assets/images/${item.overdue!?'out':'normal'}.png",width: 60.w,fit: BoxFit.fitWidth,),
        ),
      ],
    );
  }

  _searchBarWidget(){
    return Container(
        decoration:BoxDecoration(
          color: Colors.grey[200],
          borderRadius: BorderRadius.all(Radius.circular(16.r)),
        ),
        padding: EdgeInsets.only(left: 4.w,right: 1.5.w,top: 1.5.h,bottom: 1.5.h),
        margin:  EdgeInsets.only(left: 12.w, right: 12.w,top: 10.h,bottom: 10.h),
        child: Row(
          children: [
            const Icon(Icons.search),
            SizedBox(width: 4.w,),
            SizedBox(
              width: 164.w,
              child: TextField(
                  minLines: 1,
                  textInputAction: TextInputAction.search,
                  onChanged: (input){
                    keyword = input;
                    filterList(keyword);
                  },
                  decoration: InputDecoration(
                    contentPadding: EdgeInsets.only(top: 6.h,bottom: 6.h),
                    isCollapsed: true,
                    border: InputBorder.none,
                    hintText: "搜索书籍名称 书籍编号等",
                  ),
                  style: TextStyle(color: Colors.black,fontSize: 8.sp)
              ),
            ),
          ],
        )
    );
  }
}

